package com.platform.api;

import cn.hutool.core.lang.Validator;
import com.platform.annotation.IgnoreAuth;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/official")
public class ApiOfficialController extends ApiBaseAction {

    @Autowired
    private ApiCommonArticleService articleService;

    @Autowired
    private ApiShowGoodsService showGoodsService;

    @Autowired
    private ApiNewsService newsService;

    @Autowired
    private ApiVideoService videoService;//视频

    @Autowired
    private ApiRecruitmentService recruitmentService;//企业招聘
    /**
     * 通用字典表
     */
    @Autowired
    private ApiSysMacroService macroService;

    @IgnoreAuth
    @RequestMapping("wxappMenuList")
    public Object wxappMenuList() {
        Map<String, Object> params = new HashMap<>(3);
        params.put("parent_id", 13);
        params.put("status", 1);
        params.put("sidx", "order_num");
        params.put("order", "desc");
        List<SysMacroVo> sysMacroVos = macroService.queryList(params);
        return R.ok(sysMacroVos);
    }

    @IgnoreAuth
    @RequestMapping("wxAppMenuList")
    public Object wxAppMenuList() {
        Map<String, Object> params = new HashMap<>(3);
        params.put("parent_id", 13);
        params.put("status", 1);
        params.put("sidx", "order_num");
        params.put("order", "desc");
        List<SysMacroVo> sysMacroVos = macroService.queryList(params);
        return toResponsSuccess(sysMacroVos);
    }


    /**
     * 获取企业招聘职位列表
     */
    @IgnoreAuth
    @RequestMapping("recruitmentList")
    public Object recruitmentList() {
        Map<String, Object> param = new HashMap<>();
        param.put("sidx", "id");
        param.put("order", "asc");
        List<RecruitmentVo> recruitmentVos = recruitmentService.queryList(param);

        if (recruitmentVos.size() > 0) {
            for (RecruitmentVo recruitmentVo : recruitmentVos) {
                List<String> resList = new ArrayList<>();
                String res1 = recruitmentVo.getRes1();
                String res2 = recruitmentVo.getRes2();
                String res3 = recruitmentVo.getRes3();
                String res4 = recruitmentVo.getRes4();
                String res5 = recruitmentVo.getRes5();
                String res6 = recruitmentVo.getRes6();

                if (res1 != null) {
                    resList.add(res1);
                }
                if (res2 != null) {
                    resList.add(res2);
                }
                if (res3 != null) {
                    resList.add(res3);
                }
                if (res4 != null) {
                    resList.add(res4);
                }
                if (res5 != null) {
                    resList.add(res5);
                }
                if (res6 != null) {
                    resList.add(res6);
                }

                recruitmentVo.setResList(resList);
            }
        }


        return toResponsSuccess(recruitmentVos);

    }

    /**
     * 分页获取宣传视频列表
     *
     * @param page
     * @param size
     * @return
     */
    @IgnoreAuth
    @RequestMapping("videoList")
    public Object videoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "10") Integer size) {

        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("limit", size);
        param.put("enabled", 1);//可用状态
        param.put("sidx", "id");
        param.put("order", "desc");

        Query query = new Query(param);

        List list = videoService.queryList(param);
        int total = videoService.queryTotal(query);

        ApiPageUtils pageUtil = new ApiPageUtils(list, total, query.getLimit(), query.getPage());

        return toResponsSuccess(pageUtil);
    }

    /**
     * smscode
     * 宣传视频详情
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping("videoDetail")
    public Object videoDetail(@RequestParam(value = "id") Integer id) {
        VideoVo videoVo = videoService.queryObject(id);
        return toResponsSuccess(videoVo);
    }


    /**
     * 分页获取不同类型的资讯列表
     *
     * @param type
     * @return
     */
    @IgnoreAuth
    @RequestMapping("newsList")
    public Object newsList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size,
                           @RequestParam(value = "type", defaultValue = "1") Integer type) {

        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("limit", size);
        param.put("type", type);
        param.put("sidx", "id");
        param.put("order", "desc");

        Query query = new Query(param);

        List list = newsService.queryList(param);
        int total = newsService.queryTotal(query);

        ApiPageUtils pageUtil = new ApiPageUtils(list, total, query.getLimit(), query.getPage());

        return toResponsSuccess(pageUtil);
    }

    /**
     * 资讯详情
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping("newsDetail")
    public Object newsDetail(@RequestParam(value = "id") Integer id) {
        NewsVo newsVo = newsService.queryObject(id);

        //浏览数+1
        newsVo.setViewCount(newsVo.getViewCount() + 1);
        newsService.update(newsVo);

        return toResponsSuccess(newsVo);
    }


    /**
     * 产品预览
     *
     * @return
     */
    @IgnoreAuth
    @RequestMapping("previewGoodsList")
    public Object previewGoodsList() {
        Map<String, Object> param = new HashMap<>();
        List list = showGoodsService.queryList(param);
        return toResponsSuccess(list);
    }


    /**
     * 通用图文模块
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @RequestMapping("commonArticle")
    public Object commonArticle(Integer id) {
        CommonArticleVo entity = articleService.queryObject(id);
        return toResponsSuccess(entity);
    }


}
